package algorithms.greedy

/**
 * [Greedy Florist](https://www.hackerrank.com/challenges/greedy-florist)
 */
// Complete the getMinimumCost function below.
fun getMinimumCost(k: Int, c: Array<Int>): Int {
    if (c.size <= k) {
        return c.sum()
    }
    val costList = c.sorted()
    var n = c.size
    var factor = 1
    var cost = 0
    var i = costList.lastIndex

    while (i >= 0) {
        for (j in 0 until k) {
            cost += costList[i--] * factor
            if (i < 0) {
                break
            }
        }
        factor++
    }

    return cost
}